package 其他.SimpleDateFormat;

/**
 * TODO 添加类的描述
 *
 * @author zd
 * @version 1.0, 2023/7/13 17:38
 */
import java.sql.Time;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class ReminderUtils {


    public static Date calcNextRemindTime(Integer remindFrequency, String remindDay, Time remindTime) {
        Date nextRemindTime = null;
        Calendar calendar = Calendar.getInstance();

        // 设置提醒时间
        Calendar remindCalendar = Calendar.getInstance();
        remindCalendar.setTime(remindTime);

        // 根据提醒频率选择不同的计算方式
        switch (remindFrequency) {
            case 0: // 一次
                nextRemindTime = calculateOneTimeReminder(calendar, remindCalendar);
                break;
            case 1: // 每天
                nextRemindTime = calculateDailyReminder(calendar, remindCalendar);
                break;
            case 2: // 每周
                int dayOfWeek = Integer.parseInt(remindDay);
                nextRemindTime = calculateWeeklyReminder(calendar, remindCalendar, dayOfWeek);
                break;
            case 3: // 每月
                int dayOfMonth = Integer.parseInt(remindDay);
                nextRemindTime = calculateMonthlyReminder(calendar, remindCalendar, dayOfMonth);
                break;
            default:
                break;
        }

        return nextRemindTime;
    }

    private static Date calculateOneTimeReminder(Calendar calendar, Calendar remindCalendar) {
        // 获取当前时间
        Date currentTime = calendar.getTime();

        // 设置提醒时间
        Date remindTime = remindCalendar.getTime();

        // 如果提醒时间已经过去，则返回下一个时间为提醒时间
        if (remindTime.before(currentTime)) {
            return remindTime;
        } else {
            return null;
        }
    }

    private static Date calculateDailyReminder(Calendar calendar, Calendar remindCalendar) {
        // 获取当前时间
        Date currentTime = calendar.getTime();

        // 设置提醒时间
        int remindHour = remindCalendar.get(Calendar.HOUR_OF_DAY);
        int remindMinute = remindCalendar.get(Calendar.MINUTE);
        int remindSecond = remindCalendar.get(Calendar.SECOND);

        calendar.set(Calendar.HOUR_OF_DAY, remindHour);
        calendar.set(Calendar.MINUTE, remindMinute);
        calendar.set(Calendar.SECOND, remindSecond);

        // 如果提醒时间已经过去，则计算下一个提醒时间为明天
        if (calendar.getTime().before(currentTime)) {
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }

        return calendar.getTime();
    }

    private static Date calculateWeeklyReminder(Calendar calendar, Calendar remindCalendar, int dayOfWeek) {
        // 获取当前时间
        Date currentTime = calendar.getTime();

        // 设置提醒时间
        int remindHour = remindCalendar.get(Calendar.HOUR_OF_DAY);
        int remindMinute = remindCalendar.get(Calendar.MINUTE);
        int remindSecond = remindCalendar.get(Calendar.SECOND);

        calendar.set(Calendar.HOUR_OF_DAY, remindHour);
        calendar.set(Calendar.MINUTE, remindMinute);
        calendar.set(Calendar.SECOND, remindSecond);

        // 如果提醒时间已经过去，则计算下一个提醒时间为本周dayOfWeek的时间
        if (calendar.getTime().before(currentTime)) {
            int currentDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

            // 计算下一个符合条件的提醒时间
            int daysUntilNextReminder = (dayOfWeek - currentDayOfWeek + 7) % 7;
            calendar.add(Calendar.DAY_OF_MONTH, daysUntilNextReminder);
        } else if (calendar.get(Calendar.DAY_OF_WEEK) != dayOfWeek) {
            // 如果当前不是周dayOfWeek，则计算下一个符合条件的提醒时间
            int currentDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            int daysUntilNextReminder = (dayOfWeek - currentDayOfWeek + 7) % 7;
            calendar.add(Calendar.DAY_OF_MONTH, daysUntilNextReminder);
        }

        return calendar.getTime();
    }

    private static Date calculateMonthlyReminder(Calendar calendar, Calendar remindCalendar, int dayOfMonth) {
        // 获取当前时间
        Date currentTime = calendar.getTime();

        // 设置提醒时间
        int remindHour = remindCalendar.get(Calendar.HOUR_OF_DAY);
        int remindMinute = remindCalendar.get(Calendar.MINUTE);
        int remindSecond = remindCalendar.get(Calendar.SECOND);

        calendar.set(Calendar.HOUR_OF_DAY, remindHour);
        calendar.set(Calendar.MINUTE, remindMinute);
        calendar.set(Calendar.SECOND, remindSecond);

        // 如果提醒时间已经过去，则计算下一个提醒时间为本月dayOfMonth号的时间
        if (calendar.getTime().before(currentTime)) {
            int currentDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

            // 计算下一个符合条件的提醒时间
            int daysUntilNextReminder = (dayOfMonth - currentDayOfMonth + calendar.getActualMaximum(Calendar.DAY_OF_MONTH)) % calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            calendar.add(Calendar.DAY_OF_MONTH, daysUntilNextReminder);
        } else if (calendar.get(Calendar.DAY_OF_MONTH) != dayOfMonth) {
            // 如果当前不是dayOfMonth号，则计算下一个符合条件的提醒时间
            int currentDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
            int daysUntilNextReminder = (dayOfMonth - currentDayOfMonth + calendar.getActualMaximum(Calendar.DAY_OF_MONTH)) % calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            calendar.add(Calendar.DAY_OF_MONTH, daysUntilNextReminder);
        }

        return calendar.getTime();
    }
}
